package com.njtoyo.taxi.admin.handler.rule.online_valuation.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.time.LocalTime;
import java.util.Collections;
import java.util.List;

/**
 * @author mdf
 * @title: RuleDetail
 * @description: TODO
 * @date 2025/3/11 14:17
 */
@Data
public class RuleDetail {
    private Integer id;
    private String period; // 原始JSON字符串
    private String driverCommissionRule;

    @JsonIgnore // 不参与序列化的解析后数据
    private List<TimePeriod> parsedPeriods;

    // 解析时间段（懒加载）
    public List<TimePeriod> getParsedPeriods() {
        if (parsedPeriods == null) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                parsedPeriods = mapper.readValue(period,
                        new TypeReference<List<TimePeriod>>(){});
            } catch (JsonProcessingException e) {
                parsedPeriods = Collections.emptyList();
            }
        }
        return parsedPeriods;
    }

    // 检查是否匹配时间
    public boolean matchTime(LocalTime targetTime) {
        return getParsedPeriods().stream()
                .anyMatch(p -> p.contains(targetTime));
    }
}
